#include <iostream>
#include <mysql/mysql.h>
#include <unistd.h>
using namespace std;

int main()
{
    cout<<"hello"<<endl;
    MYSQL my;
    mysql_init(&my);
    /*
        链接数据库
        MYSQL *mysql_real_connect(MYSQL *mysql, //mysql操作句柄
                   const char *host,  //服务端IP地址
                   const char *user,  //用户名
                   const char *passwd, //密码
                   const char *db,     //数据库
                   unsigned int port,  //端口
                   const char *unix_socket,  //是否使用本地域套接字
                   unsigned long client_flag  //数据库标志位， 通常为0， 采用默认属性
                   ) 
    */
    MYSQL* ret = mysql_real_connect(&my, "114.132.246.245", 
    "cnl", "cnl20021123", "ChatSystem", 3306, NULL, 0);
    if(ret == NULL){
        cout<<mysql_error(&my);
        return 0;
    }
    mysql_set_character_set(&my, "utf8");
    //组织sql语句
    string sql1 = "insert into user(user_id, nickname, telnum, passwd) values(\
                    2, '于帏', '13778152272', 'cnl20021123');";
    mysql_query(&my, sql1.c_str());

    string sql2 = "select * from user";
    mysql_query(&my, sql2.c_str());

    MYSQL_RES* res = mysql_store_result(&my);
    int rows = mysql_num_rows(res);
    for(int i = 0; i < rows; ++i){
        MYSQL_ROW row = mysql_fetch_row(res);
        cout<<row[0]<<" "<<row[1]<<" "\
        <<row[2]<<" "<<row[3]<<endl;
    }
    mysql_free_result(res);
    mysql_close(&my);
    return 0;
}